package com.campus.dao;

import com.campus.dao.provider.RoleDaoDynSqlProvider;
import com.campus.pojo.SysModule;
import com.campus.pojo.SysRole;
import com.campus.tools.Tree;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface RoleDao {
    @SelectProvider(type = RoleDaoDynSqlProvider.class,method = "getAllRole")
    List<SysRole> getAllRole(Map<String ,Object> params); //分页获取角色信息

    @SelectProvider(type = RoleDaoDynSqlProvider.class,method = "count")
    int count(Map<String ,Object> params);  //获取满足条件的记录个数

    @Select("select moduleCode as id,moduleName as name, parentCode as Pid from sys_module")
    List<Tree> getModules(); //获取所有模块信息 并 映射为 tree

    @Select("select a.moduleCode as id,a.moduleName as name, a.parentCode as Pid " +
            "from sys_module as a join sys_role_module as b on a.moduleCode = b.moduleCode " +
            "where b.roleCode = #{roleCode}")
    List<Tree> getModulesByRole(@Param("roleCode") String roleCode);  //根据角色获取模块

    @Delete("delete from sys_role_module where roleCode = #{roleCode} and moduleCode = #{moduleCode}")
    int deleteRoleModules(@Param("roleCode") String roleCode,@Param("moduleCode") String moduleCode);   //删除角色模块

    @Insert("insert into sys_role_module values (#{rmId},#{roleCode},#{moduleCode},#{areaId})")
    int addRoleModules(Map<String,Object> params);  //添加

    // 查询是否存在该角色名
    @Select("select count(*) from sys_role where roleName=#{roleName} and roleCode <> #{roleCode} and areaId=#{areaId}")
    public int isExistName(@Param("roleName") String roleName, @Param("roleCode") String roleCode,
                           @Param("areaId") String areaId);

    // 添加角色
    @Insert("insert into sys_role(roleCode,roleName,areaId) values(#{roleCode},#{roleName},#{areaId})")
    public int addRole(@Param("roleCode") String roleCode, @Param("roleName") String roleName,
                       @Param("areaId") String areaId);

    // 修改
    @Update("update sys_role set roleName = #{roleName} where roleCode= #{roleCode} ")
    public int upRole(@Param("roleName") String roleName, @Param("roleCode") String roleCode);

    // 删除
    @Delete("delete from sys_role where roleCode = #{roleCode}")
    public int delRole(@Param("roleCode") String roleCode);
}
